﻿using Networks.Handlers;
using SimpleJson;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Networks
{
    public class BroadcastFactory
    {
        public const string IS_EVT_KEY_NAME = "__evt_";

        private static Dictionary<string, Action<JsonObject>> _callbacks = new Dictionary<string, Action<JsonObject>>();
        public static Dictionary<string, Action<JsonObject>> Callbacks { get => _callbacks; }

        public static void RegistEvents()
        {
            _callbacks.Add(Routes.ON_RACE_WAIT, GameHandler.onRaceWait);
            _callbacks.Add(Routes.ON_ADD, GameHandler.onAdd);
            _callbacks.Add(Routes.ON_LEAVE, GameHandler.onLeave);
            _callbacks.Add(Routes.ON_CHAT, GameHandler.onChat);
        }

        public static void InvokeHandle(string route, JsonObject data)
        {
            Action<JsonObject> handler;
            _callbacks.TryGetValue(route, out handler);
            if (handler != null)
            {
                handler(data);
            }
            else
            {
                Debug.LogWarning("cannot find handler for " + route);
            }
        }
    }
}